package com.quxian.chambercommerce.util;

import java.math.BigDecimal;
import java.util.Random;

/**
 *
 * @author Lance Chen
 */
public class StringUtil {
    
    private StringUtil() {
    }

    /**
     * Determine a string is an integer.
     * @param str The string
     * @return Return true if is integer
     */
    public static boolean isInteger(String str) {
        try {
            Integer.parseInt(str);
        } catch (Exception e) {
            return false;
        }
        return true;
    }
    public static boolean isFloat(String str) {
        try {
            Float.parseFloat(str);
        } catch (Exception e) {
            return false;
        }
        return true;
    }
    public static boolean isBigDecimal(String str){
        try {
            new BigDecimal(str);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    /**
     * Determine a string is empty.
     * @param str The given string
     * @return Return true if is empty
     */
    public static boolean isEmpty(String str) {
        if (str == null || str.trim().length() <= 0 || "null".equals(str)) {
            return true;
        }
        return false;
    }

    /**
     * Determine a string is email.
     * @param email The given string
     * @return Return true if is email
     */
    public static boolean isEmail(String email) {
        if (StringUtil.isEmpty(email)) {
            return false;
        }
        String regex = "^[\\w\\.\\-]+@([\\w\\-]+\\.)+[\\w\\-]+$";
        return email.matches(regex);
    }

    /**
     * Convert string to lower and the first character to upper.
     * @param str The string
     * @return Return string
     */
    public static String firstToUpper(String str) {
        if (isEmpty(str)) {
            return str;
        }
        str = str.trim().toLowerCase();
        str = str.substring(0, 1).toUpperCase() + str.substring(1);
        return str;
    }
    
    public static boolean equals(String src, String target) {
        if (src == null) {
            if (target == null) {
                return true;
            } else {
                return false;
            }
        } else {
            if (target == null) {
                return false;
            } else {
                return src.trim().equals(target.trim());
            }
        }
    }
    
    public static boolean isIP(String ip) {
        if (isEmpty(ip)) {
            return false;
        }
        return ip.matches("^(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])$");
    }

    /**
     * Create a random string by length, this string contains numbers and words(Upper and Lower).
     * @param length The specified string length, if length <=0, default to 6.
     * @return Return random string
     */
    public static String createRandomString(int length) {
        String codes = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        StringBuilder sb = new StringBuilder();
        Random random = new Random();
        if (length <= 0) {
            length = 6;
        }
        for (int i = 0; i < length; i ++) {
            sb.append(codes.charAt(random.nextInt(codes.length())));
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        //Create a 12 length random string
        System.out.println("Random string: " + createRandomString(12));
    }
}
